package com.feibi.study.Utils;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.Locale;
import java.util.regex.Pattern;

public class StringUtil {

    public static String stringForTime(long timeMs) {
        long totalSeconds = timeMs / 1000;
        long seconds = totalSeconds % 60;
        long minutes = (totalSeconds / 60) % 60;
        long hours = totalSeconds / 3600;
        StringBuilder mFormatBuilder = new StringBuilder();
        mFormatBuilder.setLength(0);
        Formatter mFormatter;
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    public static boolean isPassword(String content) {  //英文数字混合，至少6位，禁止其他字符
        String pattern = "^(?=.*\\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{6,18}$";
        return Pattern.matches(pattern, content);
    }

    public static boolean verPhone(String content) {
        String pattern = "^(09)\\d{8}$";
        return Pattern.matches(pattern, content);
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[0-9]*$");
        return pattern.matcher(str).matches();
    }

    public static boolean verUnityNumber(int number) {
        int sum = 0;
        int[] nums = new int[]{1, 2, 1, 2, 1, 2, 4, 1};
        if (number < 10000000 || number > 99999999) {
            return false;
        }
        for (int i = 0; i < 8; i++) {
            int posValue = (number / (int) (Math.pow(10, 7 - i))) % 10;
            int s = nums[i] * posValue;
            if (s > 9) {
                s = (s / 10) + (s % 10);
            }
            sum += s;
        }
        int pos6Value = (number / 10) % 10;
        return sum % 10 == 0 || (pos6Value == 7 && (sum + 1) % 10 == 0);
    }

    public static String subString(String content, int start, int end) {
        if (content.length() >= end) {
            return content.substring(start, end);
        } else {
            return content;
        }
    }

    public static String subString(String content, int end) {
        if (content.length() >= end) {
            return content.substring(0, end);
        } else {
            return content;
        }
    }

    public static String getStringByArray(ArrayList<String> list) {
        String result = "";
        if (list.isEmpty()) {
            return "";
        }
        for (String item : list) {
            result = result + item + "，";
        }
        if (result.length() > 1) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }


    public static String getFileName(String url) {
        if (url.contains("/")) {
            int start = url.lastIndexOf("/");
            int end = url.length();
            return url.substring(start, end);
        }
        return "";
    }
}
